﻿using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using HslCommunication.Core;
using HslCommunication.Core.Net;

namespace HslCommunication.Enthernet
{
	/// <summary>
	/// 文件管理类服务器，负责服务器所有分类文件的管理，特点是不支持文件附加数据，但是支持直接访问文件名
	/// </summary>
	/// <remarks>
	/// 本文件的服务器不支持存储文件携带的额外信息，是直接将文件存放在服务器指定目录下的，文件名不更改，特点是服务器查看方便。
	/// </remarks>
	/// <example>
	/// 以下的示例来自Demo项目，创建了一个简单的服务器对象。
	/// <code lang="cs" source="TestProject\FileNetServer\FormFileServer.cs" region="Advanced Server" title="AdvancedFileServer示例" />
	/// </example>
	// Token: 0x020000F4 RID: 244
	public class AdvancedFileServer : NetworkFileServerBase
	{
		/// <inheritdoc />
		// Token: 0x060014A4 RID: 5284 RVA: 0x0006AC9C File Offset: 0x00068E9C
		[DebuggerStepThrough]
		protected override void ThreadPoolLogin(Socket socket, IPEndPoint endPoint)
		{
			AdvancedFileServer.<ThreadPoolLogin>d__1 <ThreadPoolLogin>d__ = new AdvancedFileServer.<ThreadPoolLogin>d__1();
			<ThreadPoolLogin>d__.<>t__builder = AsyncVoidMethodBuilder.Create();
			<ThreadPoolLogin>d__.<>4__this = this;
			<ThreadPoolLogin>d__.socket = socket;
			<ThreadPoolLogin>d__.endPoint = endPoint;
			<ThreadPoolLogin>d__.<>1__state = -1;
			<ThreadPoolLogin>d__.<>t__builder.Start<AdvancedFileServer.<ThreadPoolLogin>d__1>(ref <ThreadPoolLogin>d__);
		}

		/// <inheritdoc />
		// Token: 0x060014A5 RID: 5285 RVA: 0x0006ACE4 File Offset: 0x00068EE4
		protected override void StartInitialization()
		{
			bool flag = string.IsNullOrEmpty(this.FilesDirectoryPathTemp);
			if (flag)
			{
				throw new ArgumentNullException("FilesDirectoryPathTemp", "No saved path is specified");
			}
			base.StartInitialization();
		}

		/// <inheritdoc />
		// Token: 0x060014A6 RID: 5286 RVA: 0x0006AD1C File Offset: 0x00068F1C
		protected override void CheckFolderAndCreate()
		{
			bool flag = !Directory.Exists(this.FilesDirectoryPathTemp);
			if (flag)
			{
				Directory.CreateDirectory(this.FilesDirectoryPathTemp);
			}
			base.CheckFolderAndCreate();
		}

		/// <summary>
		/// 从网络套接字接收文件并移动到目标的文件夹中，如果结果异常，则结束通讯
		/// </summary>
		/// <param name="socket"></param>
		/// <param name="savename"></param>
		/// <param name="fileNameNew"></param>
		/// <returns></returns>
		// Token: 0x060014A7 RID: 5287 RVA: 0x0006AD54 File Offset: 0x00068F54
		private OperateResult<FileBaseInfo> ReceiveFileFromSocketAndMoveFile(Socket socket, string savename, string fileNameNew)
		{
			OperateResult<FileBaseInfo> operateResult = base.ReceiveFileFromSocket(socket, savename, null);
			bool flag = !operateResult.IsSuccess;
			OperateResult<FileBaseInfo> result;
			if (flag)
			{
				base.DeleteFileByName(savename);
				result = OperateResult.CreateFailedResult<FileBaseInfo>(operateResult);
			}
			else
			{
				int num = 0;
				int i = 0;
				while (i < 3)
				{
					i++;
					bool flag2 = base.MoveFileToNewFile(savename, fileNameNew);
					if (flag2)
					{
						num = 1;
						break;
					}
					Thread.Sleep(500);
				}
				bool flag3 = num == 0;
				if (flag3)
				{
					base.DeleteFileByName(savename);
				}
				OperateResult operateResult2 = base.SendStringAndCheckReceive(socket, num, "success");
				bool flag4 = !operateResult2.IsSuccess;
				if (flag4)
				{
					result = OperateResult.CreateFailedResult<FileBaseInfo>(operateResult2);
				}
				else
				{
					result = OperateResult.CreateSuccessResult<FileBaseInfo>(operateResult.Content);
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Enthernet.AdvancedFileServer.ReceiveFileFromSocketAndMoveFile(System.Net.Sockets.Socket,System.String,System.String)" />
		// Token: 0x060014A8 RID: 5288 RVA: 0x0006AE14 File Offset: 0x00069014
		[DebuggerStepThrough]
		private Task<OperateResult<FileBaseInfo>> ReceiveFileFromSocketAndMoveFileAsync(Socket socket, string savename, string fileNameNew)
		{
			AdvancedFileServer.<ReceiveFileFromSocketAndMoveFileAsync>d__5 <ReceiveFileFromSocketAndMoveFileAsync>d__ = new AdvancedFileServer.<ReceiveFileFromSocketAndMoveFileAsync>d__5();
			<ReceiveFileFromSocketAndMoveFileAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<FileBaseInfo>>.Create();
			<ReceiveFileFromSocketAndMoveFileAsync>d__.<>4__this = this;
			<ReceiveFileFromSocketAndMoveFileAsync>d__.socket = socket;
			<ReceiveFileFromSocketAndMoveFileAsync>d__.savename = savename;
			<ReceiveFileFromSocketAndMoveFileAsync>d__.fileNameNew = fileNameNew;
			<ReceiveFileFromSocketAndMoveFileAsync>d__.<>1__state = -1;
			<ReceiveFileFromSocketAndMoveFileAsync>d__.<>t__builder.Start<AdvancedFileServer.<ReceiveFileFromSocketAndMoveFileAsync>d__5>(ref <ReceiveFileFromSocketAndMoveFileAsync>d__);
			return <ReceiveFileFromSocketAndMoveFileAsync>d__.<>t__builder.Task;
		}

		/// <summary>
		/// 用于接收上传文件时的临时文件夹，临时文件使用结束后会被删除<br />
		/// Used to receive the temporary folder when uploading files. The temporary files will be deleted after use
		/// </summary>
		// Token: 0x170004EF RID: 1263
		// (get) Token: 0x060014A9 RID: 5289 RVA: 0x0006AE70 File Offset: 0x00069070
		// (set) Token: 0x060014AA RID: 5290 RVA: 0x0006AE88 File Offset: 0x00069088
		public string FilesDirectoryPathTemp
		{
			get
			{
				return this.m_FilesDirectoryPathTemp;
			}
			set
			{
				this.m_FilesDirectoryPathTemp = base.PreprocessFolderName(value);
			}
		}

		/// <inheritdoc />
		// Token: 0x060014AB RID: 5291 RVA: 0x0006AE98 File Offset: 0x00069098
		public override string ToString()
		{
			return string.Format("AdvancedFileServer[{0}]", base.Port);
		}

		// Token: 0x040004EF RID: 1263
		private string m_FilesDirectoryPathTemp = null;
	}
}
